Central Article

Creating Offline Web Applications in Macromedia Central


Table of Contents

    1. Introduction
    2. How Central Uses Connection Awareness
    3. Caching Files and Data on the Client Machine
    4. Building a Central Application
    5. Implementing Network Status
    6. Adding Functionality with Application Controls
    7. Final Initiation
    8. Testing Your Central Application

Building a Central Application

If you haven't already done so, download and install the software in the Software Requirements section at the beginning of this article.

Creating an Interface

The example application will let you specify a file URL and cache it for offline use. When users are in offline mode, they will then be able to view, browse, and manage the caching of these files. First I'll walk through creating interface elements.

  1. Create a new fla and save it as offline_files; set your publish settings for Flash Player 6.
  2. Place an MComboBox and two MIconButtons in the upper left corner of the stage, with instance names url_cbx, add_btn, and ctrl_btn. The combobox will hold the file names of currently cached files; it will also let you to enter a URL to preview a file. The first icon button will let you add or remove the specified file from the cache, while the second icon button will allow you to preview the file without adding it to cache (as well as start and stop any sounds). This won't be a pretty interface, just a functional one that illustrates the concept of caching files for offline use.

That’s it for the interface, the rest you’ll create dynamically.

Initiating the Application

Define a function that initiates the local shared object. The shared object includes two Central DataProviders that will hold information about the files that are in the local cache and items waiting to be stored in the local cache. Put all code on the first frame of the main Timeline.

// Connect to local shared object (LSO)
var _so = SharedObject.getLocal("cachecentral", "/");

/* Create a function that is called once our application has been initiated in the Central Shell */
function initCache() {

	// Create a quick reference to the data object of the LSO
	var cache = _so.data;

	// Do this once
	if (!cache.init) {
		cache.init = true;

		/* This will hold urls of files that are waiting to be put into the local cache */
		cache._offlineQueue = new Central.DataProviderClass();

		/* This is our data provider that lists all of the files that we have available for offline viewing; used to populate the combo box */
		cache._dataProvider = new Central.DataProviderClass();
	}

	_que = cache._offlineQueue;
	_dp = cache._dataProvider;

	// Populate the combo box with a list of cached files
	url_cbx.setDataProvider(_dp);

}

Simple enough, right? The next bit of initiation code that you add lets you display the text or an image, or play a sound from the cache.

/* Create a function that is called once our application has been initiated in the Central Shell */
function initDisplay() {
    var ypos = url_cbx._y + 37;
	
	// Create an empty MovieClip and TextField and position them on the stage
	this.createEmptyMovieClip("_mc", 100);
	_mc._x = 15;
	_mc._y = ypos;
	
	this.createTextField("_txt", 101, 15, ypos, 500, 400);
	_txt.wordWrap = _txt.multiline = true;
	
	// Create a Sound object for playing back MP3s
	_sound = new Sound(this);
}

I'll explain the final bit of initiation code at the very end. This includes the onActivate function, which creates a reference to the Central Shell.